package joinpay

import (
	"encoding/json"
	"hotgo/utility/joinpay/utils"
)

const BatchPayQueryApi = "https://www.joinpay.com/payment/pay/batchPayQuery" // 批量代付查询接口

type BatchPayQueryReq struct {
	UserNo          string `json:"userNo"`
	MerchantBatchNo string `json:"merchantBatchNo"`
	Hmac            string `json:"hmac"`
}

type BatchPayQueryResp struct {
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Data       struct {
		Status          int     `json:"status"`
		ErrorCode       string  `json:"errorCode"`
		ErrorDesc       string  `json:"errorDesc"`
		UserNo          string  `json:"userNo"`
		MerchantBatchNo string  `json:"merchantBatchNo"`
		PlatformBatchNo string  `json:"platformBatchNo"`
		SuccessCount    int     `json:"successCount"`
		SuccessAmount   float64 `json:"successAmount"`
		FailCount       int     `json:"failCount"`
		FailAmount      float64 `json:"failAmount"`
		ProcessCount    int     `json:"processCount"`
		ProcessAmount   float64 `json:"processAmount"`
		RequestCount    int     `json:"requestCount"`
		RequestAmount   float64 `json:"requestAmount"`
		AcceptCount     int     `json:"acceptCount"`
		AcceptAmount    float64 `json:"acceptAmount"`
		FeeSum          float64 `json:"feeSum"`
		Hmac            string  `json:"hmac"`
		BatchList       []struct {
			Status               int     `json:"status"`
			ErrorCode            string  `json:"errorCode"`
			ErrorDesc            string  `json:"errorDesc"`
			MerchantOrderNo      string  `json:"merchantOrderNo"`
			PlatformSerialNo     string  `json:"platformSerialNo"`
			ReceiverAccountNoEnc string  `json:"receiverAccountNoEnc"`
			ReceiverNameEnc      string  `json:"receiverNameEnc"`
			PaidAmount           float64 `json:"paidAmount"`
			Fee                  float64 `json:"fee"`
		} `json:"batchList"`
	} `json:"data"`
}

func BatchPayQuery(merchantKey string, req BatchPayQueryReq) (*BatchPayQueryResp, error) {
	marshal, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	body, err := utils.PostRequest(BatchPayQueryApi, marshal)
	if err != nil {
		return nil, err
	}
	var resp *BatchPayQueryResp
	err = json.Unmarshal(body, &resp)

	return resp, nil
}
